// pages/myCollection/myCollection.js
const app = getApp()
import api from '../../utils/api.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarList: ['练习收藏', '考试收藏'],
    currentColIndex: 0,

    firstCategoryList: [],
    firstName: '全部类目',
    isShowFirstCategory: false,
    currentFirstCateIndex: 0,

    secondCategoryList: [],
    secondName: '全部分类',
    isShowSecondCategory: false,
    currentSecondCateIndex: 0,

    isShowClear: false
  },

  back() {
    wx.navigateBack({
      delta: 1
    })
  },

  changeColIndex(e) {
    this.setData({
      currentColIndex: e.currentTarget.dataset.index
    })
    this.getCollectionList()
  },

  showFirstCategory() {
    this.setData({
      isShowFirstCategory: true
    })
    api.POST('applet/SubjectFavorite/FirstCategoryList', {
        type: this.data.currentColIndex + 1
      },
      res => {
        console.log(res)
        this.setData({
          firstCategoryList: res.data.data
        })
      })
  },

  changeFirstCateIndex(e) {
    this.setData({
      currentFirstCateIndex: e.currentTarget.dataset.index,
      isShowFirstCategory: false,
      firstName: this.data.firstCategoryList[e.currentTarget.dataset.index].name
    })
    this.getCollectionList()
  },

  showSecondCategory() {
    if (!this.data.firstCategoryList[this.data.currentFirstCateIndex]) {
      wx.showToast({
        title: '请先选择上一级分类',
        icon:"none"
      })
    } else {
      api.POST('applet/SubjectFavorite/SecondCategoryList', {
        type: this.data.currentColIndex + 1,
        p_id: this.data.firstCategoryList[this.data.currentFirstCateIndex].paper_category_id
      }, res => {
        console.log(res)
        this.setData({
          secondCategoryList: res.data.data
        })
      })

      this.setData({
        isShowSecondCategory: true
      })
    }

  },

  changeSenondCateIndex(e) {
    this.setData({
      currentSecondCateIndex: e.currentTarget.dataset.index,
      isShowSecondCategory: false,
      secondName: this.data.secondCategoryList[e.currentTarget.dataset.index].name
    })
    this.getCollectionList()
  },

  getCollectionList() {
    let firstCate = this.data.firstCategoryList[this.data.currentFirstCateIndex]
    let secondCate = this.data.secondCategoryList[this.data.currentSecondCateIndex]
    wx.showLoading({
      title: '加载中',
    })
    api.POST('applet/SubjectFavorite/paperList',{
      type: this.data.currentColIndex + 1,
      p_id: firstCate ? firstCate.paper_category_id : "",
      paper_category_id: secondCate ? secondCate.paper_category_id : ""
    },res=>{
      console.log(res)
      this.setData({
        collectionList: res.data.data
      })
      wx.hideLoading()
    })
  },

  goToColDetail(e) {
    wx.navigateTo({
      url: '/pages/myColDetail/myColDetail?paper_id=' + e.currentTarget.dataset.paper_id + '&name=' + e.currentTarget.dataset.name + '&type=' + (this.data.currentColIndex + 1),
    })
  },

  showClear() {
    this.setData({
      isShowClear: true
    })
  },
  cancel() {
    this.setData({
      isShowClear: false
    })
  },
  confirm() {
    this.setData({
      isShowClear: false,
      collectionList:[]
    })
    api.POST('applet/SubjectFavorite/deleteAll',{type:this.data.currentColIndex+1},res=>{
      console.log(res)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      statusBarHeight: app.globalData.statusBarHeight
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getCollectionList()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})